// 四则运算函数
function calculate(expression) {
  // 去除空格
  expression = expression.replace(/\s+/g, '');
  
  // 检查表达式是否符合格式
  const regex = /^-?\d+(\.\d+)?([-+*/]-?\d+(\.\d+)?)?$/;
  if (!regex.test(expression)) {
      return "错误：无效的表达式";
  }

  try {
      // 使用eval函数计算表达式
      const result = eval(expression);
      return result;
  } catch (error) {
      return "错误：计算失败";
  }
}

// 测试函数
function testCalculations() {
  const testCases = [
      { input: "1+2", expected: 3 },
      { input: "3-2", expected: 1 },
      { input: "2*3", expected: 6 },
      { input: "4/2", expected: 2 },
      { input: "5/0", expected: "错误：计算失败" },
      { input: "1+2a", expected: "错误：无效的表达式" },
      { input: "1&2", expected: "错误：无效的表达式" }
  ];

  testCases.forEach(testCase => {
      const result = calculate(testCase.input);
      console.log(`测试表达式：${testCase.input}`);
      console.log(`预期结果：${testCase.expected}`);
      console.log(`实际结果：${result}`);
      console.log(`测试结果：${result === testCase.expected ? "通过" : "失败"}`);
      console.log("----------");
  });
}

// 运行测试
testCalculations();